from django.shortcuts import render
from django.shortcuts import HttpResponse,render,redirect
from django.db import models
from app01 import models
#设置index函数
def index(request):
    #ORM测试
    #ret=models.User.objects.all()#查询user类所有对象（查询user表所有记录）
    #print(ret,type(ret))
    #ret=models.User.objects.get(password='123')#获取多条数据
    #ret=models.User.objects.get(password='1234')#未获取数据
    ret=models.User.objects.filter(password='ww')#获取多条数据
    #ret=models.User.objects.filter(password='1234')#未获取数据
    print(ret)
    for i in ret:
        print(i,i.username,i.password)
    return render(request,'index.html')#返回一个页面
def login(request):
    if request.method=='POST':
        #处理POST请求的逻辑
        user=request.POST.get("username")#必须用双引号
        pwd=request.POST.get("pwd")#必须使用双引号
        #进行校验
        if models.User.objects.filter(username=user,password=pwd):
        #if user == 'ww' and pwd == '123':
            #return HttpResponse('登陆成功')
            #返回静态页面
            #return render(request,'index.html')
            #页面重定向
            return redirect('/index2/')#跳转时要设置为路径
    return render(request,'login.html')
# Create your views here.
def register(request):
    if request.method == 'POST':
        user = request.POST.get("username",None)
        pwd = request.POST.get("pwd",None)
        user_list = models.User.objects.filter(username=user)
        error_name = []
        if user_list:
            error_name = '用户名已经存在'
            return render(request,'register.html',{'error_name':error_name})
        else:
            username = models.User.objects.create(username=user,password=pwd)
            username.save()
            return redirect('/login/')
    return render(request,'register.html')

def um(request):
    data_list={}
    search_data= request.GET.get('val',"")
    if search_data:
        data_list["username__contains"]=search_data
    bks = models.User.objects.filter(**data_list)
    context={
      "search" : search_data,
      "bks" : bks
}
    bks=models.User.objects.all()
    return render(request,'um.html',context)    

def index2(request):
    return render(request,'index2.html')

def umadd(request):
    error=''
    if request.method=='POST':
        bk_username = request.POST.get("bk_username")
        bk_password = request.POST.get("bk_password")
       # pb_id = request.POST.get("pb_pk")
        if not bk_username:
            error='用户信息不能为空'
        elif models.User.objects.filter(username=bk_username):
            error='用户信息已存在'
        else:
            models.User.objects.create(username=bk_username,password=bk_password)#,db_id=pb_id
            return redirect('/um/')

   # pbs=models.Db.objects.all()

    return render(request,'umadd.html',{'error':error})#'pbs':pbs,

def umdel(request):
    pk = request.GET.get('pk')
    models.User.objects.filter(pk=pk).delete()
    return redirect('/um/')


def umedit(request):
    pk=request.GET.get('pk')
    bk_obj=models.User.objects.get(pk=pk)
    
   
    if request.method=='POST':
        bk_username=request.POST.get("bk_username")
        bk_password=request.POST.get("bk_password")
  
        bk_obj.username = bk_username
        bk_obj.password = bk_password
       
        bk_obj.save()
        return redirect('/um/')

    
  
    return render(request,'umedit.html',{'bk_obj':bk_obj})



def hadd(request):
    error=''
    if request.method=='POST':
        bk_da = request.POST.get("bk_da")
        bk_ta = request.POST.get("bk_ta")
        bk_time = request.POST.get("bk_time")
       # pb_id = request.POST.get("pb_pk")
        if not bk_time:
            error='用户信息不能为空'
        elif models.Hy.objects.filter(time=bk_time):
            error='用户信息已存在'
        else:
            models.Hy.objects.create(da=bk_da,ta=bk_ta,time=bk_time)#,db_id=pb_id
            return redirect('/hy/')

   # pbs=models.Db.objects.all()

    return render(request,'hadd.html',{'error':error})#'pbs':pbs,

def hdel(request):
    pk = request.GET.get('pk')
    models.Hy.objects.filter(pk=pk).delete()
    return redirect('/hy/')


def hedit(request):
    pk=request.GET.get('pk')
    bk_obj=models.Hy.objects.get(pk=pk)
    
   
    if request.method=='POST':
        bk_da=request.POST.get("bk_da")
        bk_ta=request.POST.get("bk_ta")
        bk_time=request.POST.get("bk_time")
  
        bk_obj.da = bk_da
        bk_obj.ta = bk_ta
        bk_obj.time = bk_time
       
        bk_obj.save()
        return redirect('/hy/')

    
  
    return render(request,'hedit.html',{'bk_obj':bk_obj})


def hy(request):
    data_list={}
    search_data= request.GET.get('val',"")
    if search_data:
        data_list["username__contains"]=search_data
    bks = models.Hy.objects.filter(**data_list)
    context={
      "search" : search_data,
      "bks" : bks
}
    bks=models.Hy.objects.all()
    return render(request,'hy.html',context)    
def shuju(request):
    data_list={}
    search_data= request.GET.get('val',"")
    if search_data:
        data_list["username__contains"]=search_data
    bks = models.Hy.objects.filter(**data_list)
    context={
      "search" : search_data,
      "bks" : bks
}
    bks=models.Hy.objects.all()
    return render(request,'shuju.html',context) 